﻿using System;
using System.Collections;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Web;
using System.Web.SessionState;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.HtmlControls;
using DataLib.Accounts.Bus;


namespace DataLib.Accounts
{
    public partial class RoleAssignment : System.Web.UI.Page
    {

        

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!Page.IsPostBack)
            {
                int userID = 0;
                User currentUser = null;
                string s = "";

                if (Request["UserID"] != null && Request["UserID"].Trim() != "")
                {
                    s = Request["UserID"];
                    userID = int.Parse(Request["UserID"]);
                }

                currentUser = new User(userID);

                Label1.Text = "为用户: " + currentUser.UserName + " 分配角色";
                if (!Page.IsPostBack)
                {
                    DataSet dsRole = AccountsTool.GetRoleList();
                    CheckBoxList1.DataSource = dsRole.Tables[0].DefaultView;
                    CheckBoxList1.DataTextField = "Description";
                    CheckBoxList1.DataValueField = "RoleID";
                    CheckBoxList1.DataBind();
                    string rolename = "";
                    AccountsPrincipal newUser = new AccountsPrincipal(currentUser.UserName);

                    if (newUser.Roles.Count > 0)
                    {
                        ArrayList roles = newUser.Roles;
                        for (int i = 0; i < roles.Count; i++)
                        {
                           
                            foreach (ListItem item in CheckBoxList1.Items)
                            {
                                Hashtable ht = (Hashtable)roles[i];
                                //rolename = ((DictionaryEntry)roles[i]).Value.ToString();
                                if(ht.ContainsValue(item.Text))
                                {
                                    item.Selected = true;
                                }
                                //if (item.Text == rolename) 
                                //    item.Selected = true;
                            }
                        }
                    }

                    if (newUser.PermissionsDesc.Count > 0)
                    {
                        RoleList.Visible = true;
                        RoleList.Text = "用户拥有的权限列表：<ul>";
                        foreach (string perm in newUser.PermissionsDesc)
                        {
                            RoleList.Text += "<li>" + perm + "</li>";
                        }
                        RoleList.Text += "</ul>";
                    }
                }
            }
        }

        #region Web 窗体设计器生成的代码
        override protected void OnInit(EventArgs e)
        {
            //
            // CODEGEN: 该调用是 ASP.NET Web 窗体设计器所必需的。
            //
            InitializeComponent();
            base.OnInit(e);
        }

        /// <summary>
        /// 设计器支持所需的方法 - 不要使用代码编辑器修改
        /// 此方法的内容。
        /// </summary>
        private void InitializeComponent()
        {
            this.BtnOk.Click += new System.Web.UI.ImageClickEventHandler(this.BtnOk_Click);
            this.Btnback.Click += new System.Web.UI.ImageClickEventHandler(this.Btnback_Click);

        }
        #endregion

        protected void BtnOk_Click(object sender, ImageClickEventArgs e)
        {


            foreach (ListItem item in CheckBoxList1.Items)
            {
                int userID = 0;
                User currentUser = null;

                if (Request["UserID"] != null && Request["UserID"].Trim() != "")
                {
                    userID = int.Parse(Request["UserID"]);
                }
                currentUser = new User(userID);

                if (item.Selected == true)
                {
                    currentUser.AddToRole(Convert.ToInt32(item.Value));
                }
                else
                {
                    currentUser.RemoveRole(Convert.ToInt32(item.Value));
                }
            }
            Response.Redirect("UserAdminList.aspx");
        }

        protected void Btnback_Click(object sender, ImageClickEventArgs e)
        {
            Response.Redirect("UserAdminList.aspx");
        }
    }
}